
import { loginByUsernameAndPassword, getMeunCodes, getAtomicCodes, getMenuRender } from "@/api/permission";
import { Message } from "element-ui";
import VueRouter from "@/router/";

const state = {
  uid: "",

  // 名称
  name: '',

  // 信息
  introduction: '',

  // 菜单权限代码
  menuCode: [],

  // 原子权限代码
  atomicCode: [],

  // 当前系统
  sys: {
    insysid: 0,
    logo: "",
    remark: "",
    shortcpname: "",
    sysname: "",
  },

  // 从服务器获取的菜单列表
  menuList: [],
}

const mutations = {
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_MENU_CODE: (state, menuCode) => {
    state.menuCode = menuCode
  },

  SET_ATOMIC_CODE: (state, atomicCode) => {
    state.atomicCode = atomicCode
  },

  SET_SYS: (state, sys) => {
    state.sys = sys;
  },

  SET_MENU_LIST: (state, list) => {
    state.menuList = list;
  },

  RESET_SYS: (state) => {
    state.sys = {
      insysid: 0,
      logo: "",
      remark: "",
      shortcpname: "",
      sysname: "",
    }
  }
}

const actions = {

  /**
   * 用户登录，以及记录当前系统
   * @param {*} param0
   * @param {*} param1
   * @return {Promise<boolean>}
   */
  async login({ commit }, { username, password, code, system }) {

    const { id: uid, cnname } = await loginByUsernameAndPassword(
      username,
      password,
      code
    );

    if (uid && cnname) {
      commit("SET_UID", uid);
      commit("SET_NAME", cnname);
      commit("SET_SYS", system);
      return true;
    }
    return false;
  },

  /**
   * 获得用户的所有菜单权限和原子权限，该函数可能比较耗时
   * @param {*} param0
   */
  async getUserRoles({ commit, state }) {
    const { uid } = state;

    Message.info("正在获取菜单代码，稍等...")
    // 获取菜单权限代码
    /** @type {string[]} */
    const meunCodes = await getMeunCodes(uid);
    commit("SET_MENU_CODE", meunCodes);

    Message.info("正在获取原子权限代码，稍等...")
    /** @type {string[]} */
    const atomicCodes = await getAtomicCodes(uid);
    commit("SET_ATOMIC_CODE", atomicCodes);

    Message.info("正在获取菜单列表，稍等...")
    const menuList = await getMenuRender(uid);
    commit("SET_MENU_LIST", convertToOrders(menuList));
  },

  /**
   * 是否登录
   * @param {*} param0
   * @returns {boolean}
   */
  hasLogin({ state }) {
    const { uid, name } = state;
    return uid.length !== 0 && name.length !== 0;
  },

  hasPermissionCode({ state }) {
    const { menuCode, atomicCode } = state;
    return menuCode.length !== 0 && atomicCode.length !== 0;
  },

  logout({ commit }) {
    commit("SET_UID", "");
    commit("SET_NAME", "");
    commit("SET_MENU_CODE", []);
    commit("SET_ATOMIC_CODE", []);
    commit("SET_MENU_LIST", []);
    commit("RESET_SYS");
    VueRouter.push({ name: "Login" })
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

/**
 * 转化为简易的有序权限数组, 方便路由重排和权限过滤使用
 */
function convertToOrders(list) {
  const result = [];
  for (let i = 0; i < list.length; i++) {
    result.push({ name: list[i].name, order: i });
    if (list[i].under.length > 0) {
      // 这里铺平
      result.push(...convertToOrders(list[i].under))
    }
  }

  return result;
}
